--材料副本功能
--#include "data\config\fuben\MaterialFubenConfig.lua" once

function IsMaterialFuben( fubenId)
    for k,v in pairs(MaterialFubenCfg)do
        if fubenId == v.fubenId then
		    return true
		end
    end
	return false
end

--[[
为副本掉线恢复，获取一些数据
]]
function GetElemsForReLoginMaterialFuben( fubenPtr )
	local fubenId = Fuben.getFuBenId(fubenPtr)
	local fubenCfg = GetMaterialFubenCfgByFubenId( fubenId )
	local InitFubenRightPanel = OpenMaterialFubenRightPanel     --打开右侧面板
	local loginLog   	= "material login" 
	local loginLogOk	= "material login ok"
	return fubenCfg, InitFubenRightPanel, loginLog, loginLogOk
end

--[[   暂时屏蔽gm
function GmDoMaterialFuben( sysarg, flag, param )
	print("GmDoMaterialFuben, flag="..flag..", param="..param)
	if flag == 1 then
		if param == 0 then
			local sVar = Actor.getStaticVar(sysarg)
			sVar.materialFB = nil
		elseif param == 1 then
			SendMaterialFubenData(sysarg)
		end
	elseif flag == 2 then
		DoEnterMaterialFuben(sysarg, param)
	elseif flag == 3 then
		DoGetMaterialFubenAwards(sysarg, param)
	elseif flag == 5 then
		DoSweepMaterialFuben(sysarg, param, 1)
	elseif flag == 6 then
		DoSweepMaterialFuben(sysarg, param, 2)
	elseif flag == 7 then
		DoSweepMaterialFuben(sysarg, param, 3)
	end
end

]]

--[[打开面板
]]
function OpenMaterialFubenPanel(sysarg )
	local fubenCfg = GetMaterialFubenCfgByFubenIdx(1)
	if not fubenCfg then
		return
	end

	Actor.openDialogs(sysarg, enLinkMaterialFuBenPanel)				--打开面板
end

function EnterMaterialFuben( sysarg, packet )
	local fubenIdx = DataPack.readByte(packet)
	DoEnterMaterialFuben( sysarg, fubenIdx )
end

function GetMaterialFubenAwards( sysarg, packet )		
	local rate = DataPack.readByte(packet)   		--领奖倍率
	--print("DoGetMaterialFubenAwards rate = "..rate)
	DoGetMaterialFubenAwards( sysarg, rate )
end

function SweepMaterialFuben( sysarg, packet )
	local fubenIdx 	= DataPack.readByte(packet)
	local rate 		= DataPack.readByte(packet)
	DoSweepMaterialFuben( sysarg, fubenIdx, rate )
end

--处理每日数据

--[[
function ProcMaterialFubenDailyData( sysarg )
	--print("ProcMaterialFubenDailyData...")
	local sVar = Actor.getStaticVar(sysarg)
	sVar.days.materialFB = nil
end

]]

--[[进入材料副本
fubenIdx：
  1-"光翼副本",
  2-"玄珠副本",
  3-"龙印副本",
  4-"勋章副本",
]]
function DoEnterMaterialFuben( sysarg, fubenIdx )
	if Actor.getIsDartFlag(sysarg) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EscortTips010, ttFlyTip)
		return
	end
	local fubenCfg = GetMaterialFubenCfgByFubenIdx(fubenIdx)
	if not fubenCfg then
		return
	end

	if not Actor.checkActorLevel(sysarg, fubenCfg.levelLimit[2], fubenCfg.levelLimit[1]) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.MaterialFuBen017, ttFlyTip)
		return
	end

	local enterTimes = GetEnterMaterialFubenTimes( sysarg, fubenIdx )   --已经进入的次数
	--print("DoEnterMaterialFuben, fubenIdx="..fubenIdx..", enterTimes="..enterTimes)

	if enterTimes >= fubenCfg.dailyEnterTimes then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.MaterialFuBen007,ttFlyTip)
		return
	end

	if not Actor.canTelport(sysarg, fubenCfg.sceneId) then  		--是否允许场景传送
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.MaterialFuBen001,ttFlyTip)
		return
	end

	local enterConsumes = fubenCfg.enterConsume[enterTimes+1]
	if not enterConsumes then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.MaterialFuBen007,ttFlyTip)
		return
	end


	if not CheckConsumeCond( sysarg, enterConsumes, 1 ) then
		--Actor.sendTipmsg(sysarg, Lang.ScriptTips.MaterialFuBen002, ttFlyTip)
		return
	end

	if not DoConsumeCond( sysarg, enterConsumes, 1, LogIds.MaterialFuBenEnterLogId, Lang.LogTips.MaterialFuBenLog001 ) then
		--Actor.sendTipmsg(sysarg, Lang.ScriptTips.MaterialFuBen002, ttFlyTip)
		return
	end

	--创建副本
	local fubenHandle = Fuben.createFuBen(fubenCfg.fubenId)
	if fubenHandle <= 0 then 		--创建副本失败
		return
	end

	SetEnterMaterialFubenTimes( sysarg, fubenIdx, enterTimes+1 )
	if Actor.enterFuBen(sysarg, fubenHandle, fubenCfg.sceneId, fubenCfg.enterPos[1], fubenCfg.enterPos[2]) then
		local fubenPtr = Actor.getFubenPrt( sysarg )
		local actorId  = Actor.getActorId( sysarg )
		Fuben.setOwnerId( fubenPtr, actorId )				--便于掉线恢复的检查

		Actor.triggerQuestEvent(sysarg, 45, 1, 1)
		local dVar = Actor.getDyanmicVar(sysarg)
		if not dVar.materialAwards then
			dVar.materialAwards = {}
		end
		Actor.closeDialogs(sysarg, enLinkMaterialFuBenPanel)	--关闭之前的面板
		FreshMaterialFubenMonsters( fubenHandle, fubenCfg.monsters )
		OpenMaterialFubenRightPanel(sysarg, fubenCfg)     --打开右侧面板
		SetMaterialFubenAwardTimes( sysarg, fubenIdx, 0 )     --将副本奖励设置为未领取
		SendActivityLog(sysarg, ACTIVITY_DEFINE_MATERIAL, ActLogStatus_Join)
	end
end

--获取领取某个副本奖励的次数
function GetMaterialFubenAwardTimes( sysarg, fubenIdx )
	local dVar = Actor.getDyanmicVar(sysarg)
	if not dVar.materialAwards then
		dVar.materialAwards = {}
	end
	local times = dVar.materialAwards[fubenIdx] or 0
	return times
end

--设置领取某个副本奖励的次数
function SetMaterialFubenAwardTimes( sysarg, fubenIdx, newTimes )
	--print("SetMaterialFubenAwardTimes, fubenIdx="..fubenIdx..", newTimes="..newTimes)
	local dVar = Actor.getDyanmicVar(sysarg)
	if not dVar.materialAwards then
		dVar.materialAwards = {}
	end
	dVar.materialAwards[fubenIdx] = newTimes
end

function GetEnterMaterialFubenTimes( sysarg, fubenIdx )
	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.days then
		sVar.days = {}
	end
	if not sVar.days.materialFB then
		sVar.days.materialFB = {}
	end
	local times = sVar.days.materialFB[fubenIdx] or 0
	return times
end

function SetEnterMaterialFubenTimes( sysarg, fubenIdx, newTimes )
	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.days then
		sVar.days = {}
	end
	if not sVar.days.materialFB then
		sVar.days.materialFB = {}
	end
	sVar.days.materialFB[fubenIdx] = newTimes
end

--刷出怪物
function FreshMaterialFubenMonsters( fubenHandle, monsters )
	for i, monCfg in ipairs( monsters) do
		freshFuBenMonsterInRange( fubenHandle, monCfg )
	end
end

--[[
领取副本奖励
awardRate：1-单倍，2-双倍，3-三倍
]]
function DoGetMaterialFubenAwards( sysarg, rate )
	--print("DoGetMaterialFubenAwards rate = "..rate)
	local fubenPtr 	= Actor.getFubenPrt(sysarg)
	if not fubenPtr then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.MaterialFuBen013, ttFlyTip)
		return
	end

	local fubenId   = Actor.getFubenId(sysarg)
	local fubenCfg  = GetMaterialFubenCfgByFubenId( fubenId )
	if not fubenCfg then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.MaterialFuBen013, ttFlyTip)
		return
	end

	if not Actor.checkActorLevel(sysarg, fubenCfg.levelLimit[2], fubenCfg.levelLimit[1]) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.MaterialFuBen018, ttFlyTip)
		return
	end

	local fubenAwards   = GetMaterialFubenAward( sysarg, fubenCfg, rate )
	local awardConsumes = fubenCfg.getAwardConsume[rate]
	if not fubenAwards then
		return
	end

	if not awardConsumes then
		return
	end

	--领取材料副本奖励的次数
	local getAwardTimes = GetMaterialFubenAwardTimes( sysarg, fubenCfg.fubenIdx )
	--print("getAwardTimes")
	if getAwardTimes > 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.MaterialFuBen014, ttFlyTip)
		return
	end

	local monNumNow = GetMaterialFubenMonsterNum( fubenPtr )
	if monNumNow > 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.MaterialFuBen012, ttFlyTip)
		return
	end

	if not IsBagGridEnough(sysarg, fubenAwards) then  		--检查背包空间
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0015, ttFlyTip)
		return
	end

	if not CheckConsumeCond( sysarg, awardConsumes, 1 ) then
		--Actor.sendTipmsg(sysarg, Lang.ScriptTips.MaterialFuBen005, ttFlyTip)
		return
	end

	if not DoConsumeCond( sysarg, awardConsumes, 1, LogIds.MaterialFuBenAwardCostLogId, Lang.LogTips.MaterialFuBenLog002 ) then
		--Actor.sendTipmsg(sysarg, Lang.ScriptTips.MaterialFuBen005, ttFlyTip)
		return
	end

	SetMaterialFubenAwardTimes( sysarg, fubenCfg.fubenIdx, getAwardTimes+1 )
	GiveCommonAward(sysarg, fubenAwards, LogIds.MaterialFuBenGetAwardLogId, Lang.LogTips.MaterialFuBenLog003)
	ExitMaterialFuben(sysarg)			--领取完毕退出副本
	SendActivityLog(sysarg, ACTIVITY_DEFINE_MATERIAL, ActLogStatus_Complete)
end

--[[
扫荡副本获得奖励
rate:扫荡倍率
扫荡占用一次进入副本的次数
]]
function DoSweepMaterialFuben( sysarg, fubenIdx, rate )
	--print("DoSweepMaterialFuben, fubenIdx="..fubenIdx..", rate="..rate)
	local fubenCfg = GetMaterialFubenCfgByFubenIdx(fubenIdx)
	if not fubenCfg then
		return
	end

	--[[
	if not IsConcVip( sysarg ) then     --非钻石贵族，不能扫荡
		Actor.sendTipmsg(sysarg, Lang.
ScriptTips.MaterialFuBen020,ttFlyTip)
		return
	end
	]]
	local needVipGrade = GetVipLevelByPrivileValue(sysarg, eVipSweepMaterialFuben,1) or 0
	
	if Actor.getVipGrade(sysarg) < needVipGrade  then     --VIP等级不够
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.MaterialFuBen020, needVipGrade), ttFlyTip)
		return
	end

	if not Actor.checkActorLevel(sysarg, fubenCfg.levelLimit[2], fubenCfg.levelLimit[1]) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.MaterialFuBen019, ttFlyTip)
		return
	end

	local enterTimes = GetEnterMaterialFubenTimes( sysarg, fubenIdx )   --已经进入的次数
	--print("DoSweepMaterialFuben, fubenIdx="..fubenIdx..", enterTimes="..enterTimes)

	if enterTimes >= fubenCfg.dailyEnterTimes then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.MaterialFuBen015,ttFlyTip)
		return
	end

	local fubenAwards   = GetMaterialFubenAward( sysarg, fubenCfg, rate )
	local sweepConsumes = fubenCfg.sweepConsume[enterTimes+1][rate]    --每次扫荡的扣费不同
	if not fubenAwards then
		return
	end

	if not sweepConsumes then     --扫荡必定需要消耗
		return
	end

	if not IsBagGridEnough(sysarg, fubenAwards) then  		--检查背包空间
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.MaterialFuBen016, ttFlyTip)
		return
	end

	if not CheckConsumeCond( sysarg, sweepConsumes, 1 ) then
		--Actor.sendTipmsg(sysarg, Lang.ScriptTips.MaterialFuBen009, ttFlyTip)
		return
	end

	if not DoConsumeCond( sysarg, sweepConsumes, 1, LogIds.MaterialFuBenSweepCostLogId, Lang.LogTips.MaterialFuBenLog004 ) then
		--Actor.sendTipmsg(sysarg, Lang.ScriptTips.MaterialFuBen009, ttFlyTip)
		return
	end

	SetEnterMaterialFubenTimes( sysarg, fubenIdx, enterTimes+1 ) 	--占用1次进入副本的次数
	GiveCommonAward(sysarg, fubenAwards, LogIds.MaterialFuBenSweepAwardLogId, Lang.LogTips.MaterialFuBenLog005)
	SendMaterialFubenData(sysarg)		--进入副本次数下发到客户端
	--Actor.triggerQuestEvent(sysarg, 19, fubenCfg.sceneId, 1)
	SendActivityLog(sysarg, ACTIVITY_DEFINE_MATERIAL, ActLogStatus_Complete)
end

--[[
副本怪物死亡
]]
function OnMaterialFubenDie(monster, sysarg, monId)
	--print("OnMaterialFubenDie, monId="..monId)
	ChangeMaterialFubenRightPanel( sysarg )			--刷新面板内容
end

--下发材料副本数据
function SendMaterialFubenData( sysarg )
	local npack = DataPack.allocPacket(sysarg,LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sMaterialCopyInfo)
	if not npack then
		return
	end

	DataPack.writeByte(npack, #MaterialFubenCfg)
	for fubenIdx, fubenCfg in ipairs(MaterialFubenCfg) do
		local enterTimes = GetEnterMaterialFubenTimes( sysarg, fubenIdx )
		DataPack.writeByte(npack, fubenIdx)
		DataPack.writeByte(npack, enterTimes)
		--print("SendMaterialFubenData, fubenIdx="..fubenIdx..", enterTimes="..enterTimes)
	end
	DataPack.flush(npack) 
end

--[[
判断是否在材料副本中
]]
function IsInMaterialFuben( fubenId )
	--print("IsInMaterialFuben, fubenId="..fubenId)
	if fubenId <= 0 then
		return false
	end
	local fubenCfg  = GetMaterialFubenCfgByFubenId( fubenId )
	if not fubenCfg then
		return false
	end
	return true
end

--[[
手工退出或者领奖后退出材料副本
退出后会触发OnExitScene
ScriptTips()，然后触发ProcExitMaterialFuben()
]]
function ExitMaterialFuben( sysarg )
	Actor.exitFuben(sysarg)					--真正退出副本
end

--退出副本成功之后触发
function OnExitMaterialFubenHandle(sysarg)
	CloseActivityRightPanel( sysarg )
end

--[[
使用回城卷，退出材料副本并回城
退出后会触发OnExitScene
ScriptTips()，然后触发ProcExitMaterialFuben()
]]
function OnMaterialFubenReturnCity( sysarg )
	--print("OnMaterialFubenReturnCity...")
	local fubenId 	= Actor.getFubenId(sysarg)
	local fubenPtr 	= Actor.getFubenPrt(sysarg)
	if not fubenPtr then
		return
	end

	local fubenCfg  = GetMaterialFubenCfgByFubenId( fubenId )
	if not fubenCfg then
		return
	end

	local monNumAll = GetMaterialFubenMonsterAllNum( fubenId )     	--配置怪物数量
	local monNumNow = GetMaterialFubenMonsterNum( fubenPtr )		--剩余怪物数量
	if monNumAll > 0 and monNumNow == 0 then    --怪物击杀完毕
		SendMaterialFubenAward( sysarg, fubenCfg, 1 )    --按单倍发奖
	end	
	local fubenHandle = Fuben.getFubenHandle(fubenPtr)
	Fuben.setFubenTime( fubenHandle, 1 )	
	Actor.exitFuben( sysarg )	--真正退出副本，并回城
end

function ProcExitMaterialFuben( sysarg )
	--print("ProcExitMaterialFuben...")
	CloseActivityRightPanel( sysarg )		--关闭右侧面板
end

--[[
材料副本时间到,会被自动踢出副本
]]
function OnMaterialFubenTimeout( fubenPtr )
	local fubenId = Fuben.getFuBenId( fubenPtr )
	local fubenCfg  = GetMaterialFubenCfgByFubenId( fubenId )
	if not fubenCfg then
		return
	end
	--CloseActivityRightPanel( sysarg )
	local monNumAll = GetMaterialFubenMonsterAllNum( fubenId )     	--配置怪物数量
	local monNumNow = GetMaterialFubenMonsterNum( fubenPtr )		--剩余怪物数量
	--print("OnMaterialFubenTimeout,  monNumNow="..monNumNow)

	if monNumAll > 0 and monNumNow == 0 then		--怪物击杀完毕
		local actorList = LuaHelp.getFbActorList(fubenPtr)
	    for k, player in ipairs( actorList or {} ) do
		    SendMaterialFubenAward( player, fubenCfg, 1 )		--按单倍发奖
	    end

	    --local fubenHandle = Fuben.getFubenHandle(fubenPtr)
	    --Fuben.setFubenTime( fubenHandle, 1 )	
	end
end

--[[
玩家下线
]]
function OnMaterialFubenLogout( sysarg )
	--print("OnMaterialFubenLogout, ...")
	local fubenId = Actor.getFubenId(sysarg)
	local fubenPtr 	= Actor.getFubenPrt(sysarg)
	if not fubenPtr then
		return
	end

	local fubenCfg  = GetMaterialFubenCfgByFubenId( fubenId )
	if not fubenCfg then
		return
	end

	local monNumAll = GetMaterialFubenMonsterAllNum( fubenId )     	--配置怪物数量
	local monNumNow = GetMaterialFubenMonsterNum( fubenPtr )		--剩余怪物数量
	if monNumAll > 0 and monNumNow == 0 then    --怪物击杀完毕
		SendMaterialFubenAward( sysarg, fubenCfg, 1 )		--按单倍发奖

		local fubenHandle = Fuben.getFubenHandle(fubenPtr)
	    Fuben.setFubenTime( fubenHandle, 1 )	
	elseif monNumAll > 0  then
		DroplineSaveFubenHandler(sysarg)
	end	
end

--[[发送副本奖励
必须击杀副本怪物完毕，并且没有领取本次奖励才能发奖
rate:奖励倍率
]]
function SendMaterialFubenAward( sysarg, fubenCfg, rate )
	--print("SendMaterialFubenAward...")
	if not rate then
		rate = 1      --缺省单倍
	end
	local fubenAwards  = GetMaterialFubenAward( sysarg, fubenCfg, rate )
	if not fubenAwards then
		return
	end

	--防止重复发奖
	local getAwardTimes = GetMaterialFubenAwardTimes( sysarg, fubenCfg.fubenIdx )
	if getAwardTimes > 0 then
		return
	end

	SetMaterialFubenAwardTimes( sysarg, fubenCfg.fubenIdx, getAwardTimes+1 )
	local actorId 	= Actor.getActorId(sysarg)
	local title 	= Lang.ScriptTips.MaterialFuBenMailTitle
	local content 	= string.format( Lang.ScriptTips.MaterialFuBenMailCont,  fubenCfg.fubenName )

	local actorLevel  = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
	local newFubenAwards = fubenAwards   --将经验库经验转换成绝对值经验

	local logId = LogIds.MaterilaFubenMailAwardLogId
	local logStr = Lang.LogTips.MaterialFuBenLog006
	Awards.GiveToMailById(actorId, newFubenAwards, title, content, logId, logStr)
end


--[[
获取副本配置中怪物全部数量
]]
function GetMaterialFubenMonsterAllNum( fubenId , monsterId)
	local fubenCfg  = GetMaterialFubenCfgByFubenId( fubenId )
	if not fubenCfg then
		return
	end

	local allMonNum = 0
	for i,monster in ipairs(fubenCfg.monsters) do
		if monsterId and monsterId == monster.monsterId then
			return monster.num
		end
		allMonNum = allMonNum + monster.num
	end
	return allMonNum
end

--[[
获取副本中怪物数量
]]
function GetMaterialFubenMonsterNum( fubenPtr , monsterId)
	local fubenId = Fuben.getFuBenId( fubenPtr )
	local fubenCfg  = GetMaterialFubenCfgByFubenId( fubenId )
	if not fubenCfg then
		return
	end

	local fubenHandle  	= Fuben.getFubenHandle(fubenPtr)
	local sceneHandle 	= Fuben.getSceneHandleById(fubenCfg.sceneId, fubenHandle)
	local monsterNum 	= 0
	for i,monster in ipairs( fubenCfg.monsters ) do
		local curNum = Fuben.getMyMonsterCount( sceneHandle, monster.monsterId )
		if monsterId and monsterId == monster.monsterId then
			return curNum
		end
		monsterNum = monsterNum + curNum
	end
	return monsterNum
end

function GetMaterialFubenCfgByFubenId( fubenId )
	for i, fubenCfg in ipairs(MaterialFubenCfg) do
		if fubenCfg.fubenId == fubenId then
			return fubenCfg
		end
	end
	return nil
end

function GetMaterialFubenCfgByFubenIdx( fubenIdxId )
	for i, fubenCfg in ipairs(MaterialFubenCfg) do
		if fubenCfg.fubenIdx == fubenIdxId then
			return fubenCfg
		end
	end
	return nil
end

function GetMaterialFubenCfgBySceneId( SceneId )
	for i, fubenCfg in ipairs(MaterialFubenCfg) do
		if fubenCfg.sceneId == SceneId then
			return fubenCfg
		end
	end
	return nil
end

function GetMaterialFubenAward( sysarg, fubenCfg, rate )
	local fubenAwards   = fubenCfg.fubenAwards[rate]     --
	if not fubenAwards then
		return
	end

	--local level  = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
	local openDays = System.getDaysSinceOpenServer()
	local fubenAward = GetElemByCond( fubenAwards, openDays )
	if fubenAward then
		return fubenAward.awards
	end
end

--[[
打开右侧导航面板
]]
function  OpenMaterialFubenRightPanel(sysarg, fubenCfg)
	local fubenId   = Actor.getFubenId(sysarg)
	local fubenPtr 	= Actor.getFubenPrt(sysarg)
	if not fubenPtr then
		return
	end
	
	local restTime 	= Fuben.getFbTime( fubenPtr )
	
	--print("OpenMaterialFubenRightPanel, monNumAll="..monNumAll..", monNumNow="..monNumNow..", restTime="..restTime)

	local panelInfo 	= {}
	panelInfo.title 		= fubenCfg.fubenName
	panelInfo.contents 	= {}	

	--面板内容
	panelInfo.contentTitle 	= Lang.ScriptTips.MaterialFuBenPanelContentTitle
	for i, monster in ipairs(fubenCfg.monsters) do
		local monNumAll = GetMaterialFubenMonsterAllNum( fubenId , monster.monsterId)
		local monNumNow = GetMaterialFubenMonsterNum( fubenPtr , monster.monsterId)
		local monsterName = System.getMonsterNameById(monster.monsterId)
		local content = string.format(Lang.ScriptTips.MaterialFuBenPanel01,monsterName, monNumAll-monNumNow, monNumAll)
		panelInfo.contents[i] = content
	end

	--副本奖励
	panelInfo.awardTitle 	= Lang.ScriptTips.MaterialFuBenPanelAwardTitle
	local  fubenAwards 		= GetMaterialFubenAward( sysarg, fubenCfg, 1 )    --奖励按1倍显示
	panelInfo.awards 		= fubenAwards or {}

	--剩余时间
	panelInfo.timeTitle 	= Lang.ScriptTips.MaterialFuBenPanelTimeTitle
	panelInfo.restTime		= restTime

	panelInfo.buttons = {1}
	panelInfo.sts = 1 

	monNumNow = GetMaterialFubenMonsterNum( fubenPtr )
	if monNumNow == 0 then
		panelInfo.sts = 2  			--已结束
		panelInfo.buttons = {2,3}
		panelInfo.buttonParams = {0, fubenCfg.doubleAwardButtonParam}	--下发按钮参数
	end

	panelInfo.subActivityId	= fubenCfg.fubenIdx   			--副本ID
	OpenActivityRightPanel( sysarg, 1, enRightPanelActivity_MaterialFuben, panelInfo )
end


function ChangeMaterialFubenRightPanel( sysarg )
	local fubenId   = Actor.getFubenId(sysarg)
	local fubenPtr 	= Actor.getFubenPrt(sysarg)
	if not fubenPtr then
		return
	end

	local fubenCfg  = GetMaterialFubenCfgByFubenId( fubenId )
	if not fubenCfg then
		return
	end

	local panelInfo 	= {}
	panelInfo.contents 	= {}

	local restTime 	= Fuben.getFbTime( fubenPtr )
	
	--print("ChangeMaterialFubenRightPanel, monNumAll="..monNumAll..", monNumNow="..monNumNow..", restTime="..restTime)

	for i, monster in ipairs(fubenCfg.monsters) do
		local monNumAll = GetMaterialFubenMonsterAllNum( fubenId , monster.monsterId)
		local monNumNow = GetMaterialFubenMonsterNum( fubenPtr , monster.monsterId)
		local monsterName = System.getMonsterNameById(monster.monsterId)
		local content = string.format(Lang.ScriptTips.MaterialFuBenPanel01, monsterName, monNumAll-monNumNow, monNumAll)
		panelInfo.contents[i] = content
	end
	monNumNow = GetMaterialFubenMonsterNum( fubenPtr )
	panelInfo.sts = 1  				--进行中
	panelInfo.buttons = {1}
	if monNumNow == 0 then
		panelInfo.sts = 2  			--已结束
		panelInfo.buttons = {2,3}
		panelInfo.buttonParams = {0, fubenCfg.doubleAwardButtonParam}	--下发按钮参数
	end
	panelInfo.restTime		= restTime
	ChangeActivityRightPanel( sysarg, panelInfo )
end

--[[
掉线恢复
注意，副本掉线恢复，走ProcLoginCommonFuben()方法
function OnMaterialFubenLogin(sysarg, fubenPtr)
	local fubenHandle = Fuben.getFubenHandle(fubenPtr)
	local fubenId = Fuben.getFuBenId(fubenPtr)
	local fubenCfg =GetMaterialFubenCfgByFubenId( fubenId )
	if fubenCfg then
		Actor.enterFuBen(sysarg, fubenHandle, fubenCfg.sceneId, fubenCfg.enterPos[1], fubenCfg.enterPos[2])
		if Actor.getFubenId(sysarg) ~= 0 then
			OpenMaterialFubenRightPanel(sysarg)
		end
	end
end
]]


function InitMaterialFuben()
    local len = #MaterialFubenCfg
    --print("InitMaterialFuben len="..len)
	for idx = 1, len do
		SetNeedReloginFuben( MaterialFubenCfg[idx].fubenId, GlobalLuaConfig.reloginFuben.materialFuben, GetElemsForReLoginMaterialFuben )
			--该副本是需要掉线恢复的
			
		SceneAwardDispatcher.register( MaterialFubenCfg[idx].sceneId, DoGetMaterialFubenAwards )

        SceneExitDispatcher.register( MaterialFubenCfg[idx].sceneId,  OnExitMaterialFubenHandle )

		FubenTimeoutDispatcher.register( MaterialFubenCfg[idx].fubenId, OnMaterialFubenTimeout )	

		SceneLogoutDispatcher.register( MaterialFubenCfg[idx].sceneId, OnMaterialFubenLogout )	
	end
end
